package com.seth.myapplication.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;

import com.seth.myapplication.LoginActivity;
import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Seth on 2017/12/5.
 */

public abstract class TasksBroadCast extends BroadcastReceiver {

    Context mContex;
    protected HashMap<String,User> users = new HashMap<>();
    protected ArrayList<Rewords> rewords = new ArrayList<>();
    int rewordIndex = 0;

    public TasksBroadCast(Context context){
        mContex = context;
    }

    @Override
        public void onReceive(Context context, Intent intent)
        {
            String msg = intent.getStringExtra("result");
            if("success".equals(msg)){

                rewords = (ArrayList<Rewords>) intent.getSerializableExtra("rewords");
                Iterator<Rewords> i = rewords.iterator();
                updateMaps();

            }else if("userinfo".equals(msg)){
                //添加同步互斥
                synchronized(TasksBroadCast.class) {
                    User user = (User) intent.getSerializableExtra("usrInfo");
                    Log.i("ReworsdRespond", "读取用户id" + user.getUsrId());

                    if (user != null) {
                        rewordIndex++;
                        users.put(user.getUsrId() + "", user);
                        Log.i("UPDATE_REWORDS", user.getUsrId() + user.getNickname());
                    }
                }
                if(rewordIndex==users.size()){
                    rewordIndex=0;
                    Log.i("ReworsdRespond","读取用户id完毕");

                    Set<String> keys = users.keySet();
                    Iterator<String> i = keys.iterator();
                    while (i.hasNext()){
                        final User user = users.get(i.next());

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                downloadPic(user);
                            }
                        }).start();
                    }
                    updateRewordsUsers();

                }
            }else if ("image".equals(msg)) {
                int id = intent.getIntExtra("userId", 0);
                Bitmap bitmap = intent.getParcelableExtra("bitmap");
                Iterator<Rewords> i = rewords.iterator();
                int position = 0;
                while (i.hasNext()){
                    Rewords  reword = i.next();
                    position = rewords.indexOf(reword);
                    if(reword.getUsrId()==id){
                        reword.getSeter().setBitmap(bitmap);
                    }
                }
                updateRecyclerView(position,"update_frog");
            }
            else{
                failureUpdate();
                Log.i("ReworsdRespond","失败"+msg);
                Toast.makeText(context, "获取悬赏失败", Toast.LENGTH_SHORT).show();
            }
        }


    protected abstract void updateRecyclerView(int position, String update_frog);

    protected abstract void downloadPic(User usr);

    protected abstract void failureUpdate();

    protected abstract void updateRewordsUsers();

    protected abstract void updateMaps();
}
